/**
 * 微信授权方信息
 * Created by dong on 2017/2/21.
 */
/// <reference path="./weixin-authorizer-service.d.ts" />
import * as rp from "request-promise-native";
import CONFIG from "../config";
import SiteService from "../service/site-service";
// import logger from "../util/logger";

const component_appid = CONFIG.appID;
const HOST = 'https://api.weixin.qq.com/cgi-bin';

export default class WeixinAuthorizerService {

    /**
     * 使用授权码换取公众号的接口调用凭据和授权信息
     * @param authCode
     * @returns {Promise}
     */
    static async queryAuth(authCode): Promise<object> {
        let component_access_token = await SiteService.getAccessToken();
        return rp({
            method: 'POST',
            uri: `${HOST}/component/api_query_auth`,
            qs: {
                component_access_token
            },
            body: {
                component_appid,
                authorization_code: authCode
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取（刷新）授权公众号的接口调用凭据（令牌）
     * @param authorizer_appid
     * @param authorizer_refresh_token
     * @returns {Promise}
     */
    static async refreshAuthToken({authorizer_appid, authorizer_refresh_token}): Promise<AuthorizerAccessToken> {
        return rp({
            method: 'POST',
            uri: `${HOST}/component/api_authorizer_token`,
            qs: {
                component_access_token: await SiteService.getAccessToken()
            },
            body: {
                component_appid,
                authorizer_appid,
                authorizer_refresh_token
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取授权方的公众号帐号基本信息
     * @param authorizer_appid
     * @returns {Promise}
     */
    static async getAuthorizerInfo(authorizer_appid: string): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/component/api_get_authorizer_info`,
            qs: {
                component_access_token: await SiteService.getAccessToken()
            },
            body: {
                component_appid,
                authorizer_appid
            },
            json: true
        })
        // return Promise.reject({errcode:44444,errmsg:"测试错误"})
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取授权方的选项设置信息
     * @param authorizer_appid
     * @param option_name
     * @returns {Promise<any>}
     */
    static async getAuthorizerOptionInfo({authorizer_appid, option_name}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/component/api_get_authorizer_info`,
            qs: {
                component_access_token: await SiteService.getAccessToken()
            },
            body: {
                component_appid,
                authorizer_appid,
                option_name
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 设置授权方的选项设置信息
     * @param authorizer_appid
     * @param option_name
     * @param option_value
     * @returns {Promise<any>}
     */
    static async setAuthorizerOptionInfo({authorizer_appid, option_name, option_value}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/component/api_set_authorizer_option`,
            qs: {
                component_access_token: await SiteService.getAccessToken()
            },
            body: {
                component_appid,
                authorizer_appid,
                option_name,
                option_value
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 所有API调用（包括第三方代公众号调用）次数进行清零
     * @param access_token
     * @param authorizer_appid
     * @returns {Promise<any>}
     */
    static clearQuota({access_token, authorizer_appid}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/component/clear_quota`,
            qs: {
                access_token
            },
            body: {
                authorizer_appid
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);

    }
    /**
     * 更新jsapiTicket
     * @param authorizer_access_token
     * @returns {Promise}
     */
    static getJSApiTicket(authorizer_access_token): Promise<ComponentJSApiTicket> {
        return rp({
            method: 'GET',
            uri: `${HOST}/ticket/getticket`,
            qs: { access_token:authorizer_access_token ,type:'jsapi'},
            json: true
        }).then((data: any) => (data.errcode ? Promise.reject(data) : data));
    }
}
